<?php

namespace app\common\Services;


use app\common\library\Log;
use GuzzleHttp\Client;

class SmsService
{
    public $config;

    public function __construct()
    {
        $this->config = config('sms_config');
    }

    /**
     * 生成签名
     * @param $time
     * @return string
     */
    public function sign($time)
    {
        return md5($this->config['accountSid'] . $this->config['authToken'] . $time);
    }

    /**
     * 发短信
     * @param $mobile
     * @param $code
     * @return bool
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function send($mobile,$code)
    {
        $http = new Client();
        $time = time() * 1000;
        $sign = $this->sign($time);
        $body = [
            'accountSid' => $this->config['accountSid'],
            'param' => $code,
            'to' => $mobile,
            'sig' => $sign,
            'templateid' => $this->config['templateId'],
            'timestamp' => $time,
        ];
        $response = $http->request('POST', $this->config['url'], ['form_params' => $body]);

        $result = json_decode($response->getBody()->getContents(),true);

       if($result['respCode'] !== '0000'){
           (new Log())->info('短信发送失败', $result);
           return false;
       }
       return true;



    }


}